/**
 * 字符串处理工具函数 - TypeScript版本
 * 提供字符串格式化、转换、验证、处理等功能
 * 支持直接复制粘贴使用，无需任何依赖
 * 每个函数都是独立的，可以单独复制使用
 */

// 类型定义
export interface StringOptions {
  case?: string;
  trim?: string;
  pad?: string;
  encoding?: string;
  algorithm?: string;
}

export interface StringResult {
  result: string;
  original: string;
  type: string;
}

export interface EncodingResult {
  encoded: string;
  original: string;
  algorithm: string;
}

export interface ConversionResult {
  converted: string;
  original: string;
  from: string;
  to: string;
}

// 常量定义
export const STRING_TYPES = {
  CAMEL_CASE: 'camelCase',
  SNAKE_CASE: 'snake_case',
  KEBAB_CASE: 'kebab-case',
  PASCAL_CASE: 'PascalCase',
  CONSTANT_CASE: 'CONSTANT_CASE',
  TITLE_CASE: 'Title Case',
  SENTENCE_CASE: 'Sentence case'
} as const;

export const TRIM_TYPES = {
  ALL: 'all',
  START: 'start',
  END: 'end',
  BOTH: 'both'
} as const;

export const PAD_TYPES = {
  START: 'start',
  END: 'end',
  BOTH: 'both'
} as const;

/**
 * 转换为驼峰命名
 * @param {string} str - 输入字符串
 * @returns {string} 驼峰命名
 */
export function toCamelCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => {
      return index === 0 ? word.toLowerCase() : word.toUpperCase();
    })
    .replace(/\s+/g, '');
}

/**
 * 转换为下划线命名
 * @param {string} str - 输入字符串
 * @returns {string} 下划线命名
 */
export function toSnakeCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/([A-Z])/g, '_$1')
    .replace(/[-\s]+/g, '_')
    .toLowerCase()
    .replace(/^_/, '');
}

/**
 * 转换为短横线命名
 * @param {string} str - 输入字符串
 * @returns {string} 短横线命名
 */
export function toKebabCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/([A-Z])/g, '-$1')
    .replace(/[_\s]+/g, '-')
    .toLowerCase()
    .replace(/^-/, '');
}

/**
 * 转换为帕斯卡命名
 * @param {string} str - 输入字符串
 * @returns {string} 帕斯卡命名
 */
export function toPascalCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => {
      return word.toUpperCase();
    })
    .replace(/\s+/g, '');
}

/**
 * 转换为常量命名
 * @param {string} str - 输入字符串
 * @returns {string} 常量命名
 */
export function toConstantCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/([A-Z])/g, '_$1')
    .replace(/[-\s]+/g, '_')
    .toUpperCase()
    .replace(/^_/, '');
}

/**
 * 转换为标题格式
 * @param {string} str - 输入字符串
 * @returns {string} 标题格式
 */
export function toTitleCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .toLowerCase()
    .split(' ')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
}

/**
 * 转换为句子格式
 * @param {string} str - 输入字符串
 * @returns {string} 句子格式
 */
export function toSentenceCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}

/**
 * 首字母大写
 * @param {string} str - 输入字符串
 * @returns {string} 首字母大写
 */
export function capitalize(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.charAt(0).toUpperCase() + str.slice(1);
}

/**
 * 首字母小写
 * @param {string} str - 输入字符串
 * @returns {string} 首字母小写
 */
export function uncapitalize(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.charAt(0).toLowerCase() + str.slice(1);
}

/**
 * 反转字符串
 * @param {string} str - 输入字符串
 * @returns {string} 反转后的字符串
 */
export function reverse(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.split('').reverse().join('');
}

/**
 * 字符串截取
 * @param {string} str - 输入字符串
 * @param {number} start - 开始位置
 * @param {number} length - 截取长度
 * @returns {string} 截取后的字符串
 */
export function substring(str: string, start: number, length?: number): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (length === undefined) {
    return str.substring(start);
  }
  
  return str.substring(start, start + length);
}

/**
 * 字符串截断
 * @param {string} str - 输入字符串
 * @param {number} length - 最大长度
 * @param {string} suffix - 后缀
 * @returns {string} 截断后的字符串
 */
export function truncate(str: string, length: number, suffix: string = '...'): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length <= length) {
    return str;
  }
  
  return str.substring(0, length - suffix.length) + suffix;
}

/**
 * 字符串填充
 * @param {string} str - 输入字符串
 * @param {number} length - 目标长度
 * @param {string} padString - 填充字符
 * @param {string} type - 填充类型
 * @returns {string} 填充后的字符串
 */
export function pad(str: string, length: number, padString: string = ' ', type: string = PAD_TYPES.END): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length >= length) {
    return str;
  }
  
  const padLength = length - str.length;
  const padStr = padString.repeat(Math.ceil(padLength / padString.length)).substring(0, padLength);
  
  switch (type) {
    case PAD_TYPES.START:
      return padStr + str;
    case PAD_TYPES.END:
      return str + padStr;
    case PAD_TYPES.BOTH:
      const leftPad = Math.floor(padLength / 2);
      const rightPad = padLength - leftPad;
      return padString.repeat(Math.ceil(leftPad / padString.length)).substring(0, leftPad) + 
             str + 
             padString.repeat(Math.ceil(rightPad / padString.length)).substring(0, rightPad);
    default:
      return str + padStr;
  }
}

/**
 * 字符串去空格
 * @param {string} str - 输入字符串
 * @param {string} type - 去空格类型
 * @returns {string} 去空格后的字符串
 */
export function trim(str: string, type: string = TRIM_TYPES.BOTH): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  switch (type) {
    case TRIM_TYPES.START:
      return str.replace(/^\s+/, '');
    case TRIM_TYPES.END:
      return str.replace(/\s+$/, '');
    case TRIM_TYPES.BOTH:
      return str.trim();
    case TRIM_TYPES.ALL:
      return str.replace(/\s+/g, '');
    default:
      return str.trim();
  }
}

/**
 * 字符串重复
 * @param {string} str - 输入字符串
 * @param {number} count - 重复次数
 * @returns {string} 重复后的字符串
 */
export function repeat(str: string, count: number): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (count <= 0) {
    return '';
  }
  
  return str.repeat(count);
}

/**
 * 字符串替换
 * @param {string} str - 输入字符串
 * @param {string|RegExp} searchValue - 搜索值
 * @param {string} replaceValue - 替换值
 * @param {boolean} global - 是否全局替换
 * @returns {string} 替换后的字符串
 */
export function replace(str: string, searchValue: string | RegExp, replaceValue: string, global: boolean = true): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (global && typeof searchValue === 'string') {
    return str.split(searchValue).join(replaceValue);
  }
  
  return str.replace(searchValue, replaceValue);
}

/**
 * 字符串替换所有
 * @param {string} str - 输入字符串
 * @param {string} searchValue - 搜索值
 * @param {string} replaceValue - 替换值
 * @returns {string} 替换后的字符串
 */
export function replaceAll(str: string, searchValue: string, replaceValue: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.split(searchValue).join(replaceValue);
}

/**
 * 字符串分割
 * @param {string} str - 输入字符串
 * @param {string|RegExp} separator - 分隔符
 * @param {number} limit - 限制分割数量
 * @returns {Array} 分割后的数组
 */
export function split(str: string, separator: string | RegExp, limit?: number): string[] {
  if (!str || typeof str !== 'string') {
    return [];
  }
  
  if (limit !== undefined) {
    return str.split(separator, limit);
  }
  
  return str.split(separator);
}

/**
 * 字符串连接
 * @param {Array} strings - 字符串数组
 * @param {string} separator - 分隔符
 * @returns {string} 连接后的字符串
 */
export function join(strings: string[], separator: string = ''): string {
  if (!Array.isArray(strings)) {
    return '';
  }
  
  return strings.join(separator);
}

/**
 * 字符串包含检查
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 开始位置
 * @returns {boolean} 是否包含
 */
export function includes(str: string, searchString: string, position: number = 0): boolean {
  if (!str || typeof str !== 'string') {
    return false;
  }
  
  return str.includes(searchString, position);
}

/**
 * 字符串开始检查
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 开始位置
 * @returns {boolean} 是否开始
 */
export function startsWith(str: string, searchString: string, position: number = 0): boolean {
  if (!str || typeof str !== 'string') {
    return false;
  }
  
  return str.startsWith(searchString, position);
}

/**
 * 字符串结束检查
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 结束位置
 * @returns {boolean} 是否结束
 */
export function endsWith(str: string, searchString: string, position?: number): boolean {
  if (!str || typeof str !== 'string') {
    return false;
  }
  
  return str.endsWith(searchString, position);
}

/**
 * 字符串查找
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 开始位置
 * @returns {number} 查找位置
 */
export function indexOf(str: string, searchString: string, position: number = 0): number {
  if (!str || typeof str !== 'string') {
    return -1;
  }
  
  return str.indexOf(searchString, position);
}

/**
 * 字符串最后查找
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 开始位置
 * @returns {number} 查找位置
 */
export function lastIndexOf(str: string, searchString: string, position?: number): number {
  if (!str || typeof str !== 'string') {
    return -1;
  }
  
  return str.lastIndexOf(searchString, position);
}

/**
 * 字符串匹配
 * @param {string} str - 输入字符串
 * @param {string|RegExp} pattern - 匹配模式
 * @returns {Array|null} 匹配结果
 */
export function match(str: string, pattern: string | RegExp): RegExpMatchArray | null {
  if (!str || typeof str !== 'string') {
    return null;
  }
  
  return str.match(pattern);
}

/**
 * 字符串匹配所有
 * @param {string} str - 输入字符串
 * @param {string|RegExp} pattern - 匹配模式
 * @returns {Array} 匹配结果数组
 */
export function matchAll(str: string, pattern: string | RegExp): RegExpMatchArray[] {
  if (!str || typeof str !== 'string') {
    return [];
  }
  
  if (typeof pattern === 'string') {
    pattern = new RegExp(pattern, 'g');
  }
  
  return Array.from(str.matchAll(pattern));
}

/**
 * 字符串搜索
 * @param {string} str - 输入字符串
 * @param {string|RegExp} pattern - 搜索模式
 * @returns {number} 搜索位置
 */
export function search(str: string, pattern: string | RegExp): number {
  if (!str || typeof str !== 'string') {
    return -1;
  }
  
  return str.search(pattern);
}

/**
 * 字符串提取
 * @param {string} str - 输入字符串
 * @param {number} start - 开始位置
 * @param {number} end - 结束位置
 * @returns {string} 提取的字符串
 */
export function slice(str: string, start: number, end?: number): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.slice(start, end);
}

/**
 * 字符串提取
 * @param {string} str - 输入字符串
 * @param {number} start - 开始位置
 * @param {number} length - 提取长度
 * @returns {string} 提取的字符串
 */
export function substr(str: string, start: number, length?: number): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.substr(start, length);
}

/**
 * 字符串转小写
 * @param {string} str - 输入字符串
 * @returns {string} 小写字符串
 */
export function toLowerCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.toLowerCase();
}

/**
 * 字符串转大写
 * @param {string} str - 输入字符串
 * @returns {string} 大写字符串
 */
export function toUpperCase(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.toUpperCase();
}

/**
 * 字符串转本地小写
 * @param {string} str - 输入字符串
 * @param {string} locale - 本地化
 * @returns {string} 本地小写字符串
 */
export function toLocaleLowerCase(str: string, locale?: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.toLocaleLowerCase(locale);
}

/**
 * 字符串转本地大写
 * @param {string} str - 输入字符串
 * @param {string} locale - 本地化
 * @returns {string} 本地大写字符串
 */
export function toLocaleUpperCase(str: string, locale?: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.toLocaleUpperCase(locale);
}

/**
 * 字符串转数字
 * @param {string} str - 输入字符串
 * @param {number} radix - 进制
 * @returns {number} 数字
 */
export function toNumber(str: string, radix?: number): number {
  if (!str || typeof str !== 'string') {
    return NaN;
  }
  
  if (radix !== undefined) {
    return parseInt(str, radix);
  }
  
  return parseFloat(str);
}

/**
 * 字符串转整数
 * @param {string} str - 输入字符串
 * @param {number} radix - 进制
 * @returns {number} 整数
 */
export function toInteger(str: string, radix?: number): number {
  if (!str || typeof str !== 'string') {
    return NaN;
  }
  
  return parseInt(str, radix);
}

/**
 * 字符串转浮点数
 * @param {string} str - 输入字符串
 * @returns {number} 浮点数
 */
export function toFloat(str: string): number {
  if (!str || typeof str !== 'string') {
    return NaN;
  }
  
  return parseFloat(str);
}

/**
 * 字符串转布尔值
 * @param {string} str - 输入字符串
 * @returns {boolean} 布尔值
 */
export function toBoolean(str: string): boolean {
  if (!str || typeof str !== 'string') {
    return false;
  }
  
  const lowerStr = str.toLowerCase();
  return lowerStr === 'true' || lowerStr === '1' || lowerStr === 'yes' || lowerStr === 'on';
}

/**
 * 字符串转数组
 * @param {string} str - 输入字符串
 * @param {string} separator - 分隔符
 * @returns {Array} 数组
 */
export function toArray(str: string, separator: string = ''): string[] {
  if (!str || typeof str !== 'string') {
    return [];
  }
  
  if (separator === '') {
    return str.split('');
  }
  
  return str.split(separator);
}

/**
 * 字符串转对象
 * @param {string} str - 输入字符串
 * @param {string} keySeparator - 键值分隔符
 * @param {string} pairSeparator - 对分隔符
 * @returns {Object} 对象
 */
export function toObject(str: string, keySeparator: string = '=', pairSeparator: string = '&'): Record<string, string> {
  if (!str || typeof str !== 'string') {
    return {};
  }
  
  const obj: Record<string, string> = {};
  const pairs = str.split(pairSeparator);
  
  for (const pair of pairs) {
    const [key, value] = pair.split(keySeparator);
    if (key && value !== undefined) {
      obj[key.trim()] = value.trim();
    }
  }
  
  return obj;
}

/**
 * 字符串转JSON
 * @param {string} str - 输入字符串
 * @returns {Object} JSON对象
 */
export function toJSON(str: string): any {
  if (!str || typeof str !== 'string') {
    return null;
  }
  
  try {
    return JSON.parse(str);
  } catch (error) {
    return null;
  }
}

/**
 * 字符串转Base64
 * @param {string} str - 输入字符串
 * @returns {string} Base64字符串
 */
export function toBase64(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return btoa(unescape(encodeURIComponent(str)));
}

/**
 * Base64转字符串
 * @param {string} str - Base64字符串
 * @returns {string} 原始字符串
 */
export function fromBase64(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  try {
    return decodeURIComponent(escape(atob(str)));
  } catch (error) {
    return '';
  }
}

/**
 * 字符串转URL编码
 * @param {string} str - 输入字符串
 * @returns {string} URL编码字符串
 */
export function toURLEncode(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return encodeURIComponent(str);
}

/**
 * URL编码转字符串
 * @param {string} str - URL编码字符串
 * @returns {string} 原始字符串
 */
export function fromURLDecode(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  try {
    return decodeURIComponent(str);
  } catch (error) {
    return '';
  }
}

/**
 * 字符串转HTML编码
 * @param {string} str - 输入字符串
 * @returns {string} HTML编码字符串
 */
export function toHTMLEncode(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const div = document.createElement('div');
  div.textContent = str;
  return div.innerHTML;
}

/**
 * HTML编码转字符串
 * @param {string} str - HTML编码字符串
 * @returns {string} 原始字符串
 */
export function fromHTMLDecode(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const div = document.createElement('div');
  div.innerHTML = str;
  return div.textContent || div.innerText || '';
}

/**
 * 字符串转十六进制
 * @param {string} str - 输入字符串
 * @returns {string} 十六进制字符串
 */
export function toHex(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const hex = str.charCodeAt(i).toString(16);
    result += hex.length === 1 ? '0' + hex : hex;
  }
  
  return result;
}

/**
 * 十六进制转字符串
 * @param {string} str - 十六进制字符串
 * @returns {string} 原始字符串
 */
export function fromHex(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length % 2 !== 0) {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i += 2) {
    const hex = str.substr(i, 2);
    const charCode = parseInt(hex, 16);
    result += String.fromCharCode(charCode);
  }
  
  return result;
}

/**
 * 字符串转Unicode
 * @param {string} str - 输入字符串
 * @returns {string} Unicode字符串
 */
export function toUnicode(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const code = str.charCodeAt(i);
    result += '\\u' + code.toString(16).padStart(4, '0');
  }
  
  return result;
}

/**
 * Unicode转字符串
 * @param {string} str - Unicode字符串
 * @returns {string} 原始字符串
 */
export function fromUnicode(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/\\u[\dA-Fa-f]{4}/g, (match) => {
    const code = parseInt(match.substr(2), 16);
    return String.fromCharCode(code);
  });
}

/**
 * 字符串转ASCII
 * @param {string} str - 输入字符串
 * @returns {Array} ASCII数组
 */
export function toASCII(str: string): number[] {
  if (!str || typeof str !== 'string') {
    return [];
  }
  
  const result: number[] = [];
  for (let i = 0; i < str.length; i++) {
    result.push(str.charCodeAt(i));
  }
  
  return result;
}

/**
 * ASCII转字符串
 * @param {Array} codes - ASCII数组
 * @returns {string} 字符串
 */
export function fromASCII(codes: number[]): string {
  if (!Array.isArray(codes)) {
    return '';
  }
  
  let result = '';
  for (const code of codes) {
    result += String.fromCharCode(code);
  }
  
  return result;
}

/**
 * 字符串转二进制
 * @param {string} str - 输入字符串
 * @returns {string} 二进制字符串
 */
export function toBinary(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const binary = str.charCodeAt(i).toString(2);
    result += binary.padStart(8, '0');
  }
  
  return result;
}

/**
 * 二进制转字符串
 * @param {string} str - 二进制字符串
 * @returns {string} 原始字符串
 */
export function fromBinary(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length % 8 !== 0) {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i += 8) {
    const binary = str.substr(i, 8);
    const charCode = parseInt(binary, 2);
    result += String.fromCharCode(charCode);
  }
  
  return result;
}

/**
 * 字符串转八进制
 * @param {string} str - 输入字符串
 * @returns {string} 八进制字符串
 */
export function toOctal(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const octal = str.charCodeAt(i).toString(8);
    result += octal.padStart(3, '0');
  }
  
  return result;
}

/**
 * 八进制转字符串
 * @param {string} str - 八进制字符串
 * @returns {string} 原始字符串
 */
export function fromOctal(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length % 3 !== 0) {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i += 3) {
    const octal = str.substr(i, 3);
    const charCode = parseInt(octal, 8);
    result += String.fromCharCode(charCode);
  }
  
  return result;
}

/**
 * 字符串转十进制
 * @param {string} str - 输入字符串
 * @returns {string} 十进制字符串
 */
export function toDecimal(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const decimal = str.charCodeAt(i).toString(10);
    result += decimal + ' ';
  }
  
  return result.trim();
}

/**
 * 十进制转字符串
 * @param {string} str - 十进制字符串
 * @returns {string} 原始字符串
 */
export function fromDecimal(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const codes = str.split(' ').map(code => parseInt(code, 10));
  let result = '';
  for (const code of codes) {
    if (!isNaN(code)) {
      result += String.fromCharCode(code);
    }
  }
  
  return result;
}

/**
 * 字符串转摩尔斯码
 * @param {string} str - 输入字符串
 * @returns {string} 摩尔斯码
 */
export function toMorse(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const morseCode: Record<string, string> = {
    'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.',
    'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.',
    'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-',
    'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..',
    '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
    '6': '-....', '7': '--...', '8': '---..', '9': '----.',
    ' ': ' ', '.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--',
    '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...', ';': '-.-.-.',
    '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.', '$': '...-..-',
    '@': '.--.-.'
  };
  
  return str.toUpperCase().split('').map(char => morseCode[char] || char).join(' ');
}

/**
 * 摩尔斯码转字符串
 * @param {string} str - 摩尔斯码
 * @returns {string} 原始字符串
 */
export function fromMorse(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const morseCode: Record<string, string> = {
    '.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E', '..-.': 'F', '--.': 'G',
    '....': 'H', '..': 'I', '.---': 'J', '-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N',
    '---': 'O', '.--.': 'P', '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T', '..-': 'U',
    '...-': 'V', '.--': 'W', '-..-': 'X', '-.--': 'Y', '--..': 'Z',
    '-----': '0', '.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5',
    '-....': '6', '--...': '7', '---..': '8', '----.': '9',
    ' ': ' ', '.-.-.-': '.', '--..--': ',', '..--..': '?', '.----.': "'", '-.-.--': '!',
    '-..-.': '/', '-.--.': '(', '-.--.-': ')', '.-...': '&', '---...': ':', '-.-.-.': ';',
    '-...-': '=', '.-.-.': '+', '-....-': '-', '..--.-': '_', '.-..-.': '"', '...-..-': '$',
    '.--.-.': '@'
  };
  
  return str.split(' ').map(code => morseCode[code] || code).join('');
}

/**
 * 字符串转ROT13
 * @param {string} str - 输入字符串
 * @returns {string} ROT13字符串
 */
export function toROT13(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(((code - start + 13) % 26) + start);
  });
}

/**
 * ROT13转字符串
 * @param {string} str - ROT13字符串
 * @returns {string} 原始字符串
 */
export function fromROT13(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(((code - start + 13) % 26) + start);
  });
}

/**
 * 字符串转凯撒密码
 * @param {string} str - 输入字符串
 * @param {number} shift - 偏移量
 * @returns {string} 凯撒密码
 */
export function toCaesar(str: string, shift: number = 3): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(((code - start + shift) % 26) + start);
  });
}

/**
 * 凯撒密码转字符串
 * @param {string} str - 凯撒密码
 * @param {number} shift - 偏移量
 * @returns {string} 原始字符串
 */
export function fromCaesar(str: string, shift: number = 3): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(((code - start - shift + 26) % 26) + start);
  });
}

/**
 * 字符串转Atbash密码
 * @param {string} str - 输入字符串
 * @returns {string} Atbash密码
 */
export function toAtbash(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(25 - (code - start) + start);
  });
}

/**
 * Atbash密码转字符串
 * @param {string} str - Atbash密码
 * @returns {string} 原始字符串
 */
export function fromAtbash(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(25 - (code - start) + start);
  });
}

/**
 * 字符串转Vigenère密码
 * @param {string} str - 输入字符串
 * @param {string} key - 密钥
 * @returns {string} Vigenère密码
 */
export function toVigenere(str: string, key: string): string {
  if (!str || typeof str !== 'string' || !key || typeof key !== 'string') {
    return '';
  }
  
  const keyUpper = key.toUpperCase();
  let keyIndex = 0;
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    const keyChar = keyUpper.charCodeAt(keyIndex % keyUpper.length) - 65;
    keyIndex++;
    return String.fromCharCode(((code - start + keyChar) % 26) + start);
  });
}

/**
 * Vigenère密码转字符串
 * @param {string} str - Vigenère密码
 * @param {string} key - 密钥
 * @returns {string} 原始字符串
 */
export function fromVigenere(str: string, key: string): string {
  if (!str || typeof str !== 'string' || !key || typeof key !== 'string') {
    return '';
  }
  
  const keyUpper = key.toUpperCase();
  let keyIndex = 0;
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    const keyChar = keyUpper.charCodeAt(keyIndex % keyUpper.length) - 65;
    keyIndex++;
    return String.fromCharCode(((code - start - keyChar + 26) % 26) + start);
  });
}

/**
 * 字符串转Bacon密码
 * @param {string} str - 输入字符串
 * @returns {string} Bacon密码
 */
export function toBacon(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const baconCode: Record<string, string> = {
    'A': 'AAAAA', 'B': 'AAAAB', 'C': 'AAABA', 'D': 'AAABB', 'E': 'AABAA', 'F': 'AABAB',
    'G': 'AABBA', 'H': 'AABBB', 'I': 'ABAAA', 'J': 'ABAAB', 'K': 'ABABA', 'L': 'ABABB',
    'M': 'ABBAA', 'N': 'ABBAB', 'O': 'ABBBA', 'P': 'ABBBB', 'Q': 'BAAAA', 'R': 'BAAAB',
    'S': 'BAABA', 'T': 'BAABB', 'U': 'BABAA', 'V': 'BABAB', 'W': 'BABBA', 'X': 'BABBB',
    'Y': 'BBAAA', 'Z': 'BBAAB'
  };
  
  return str.toUpperCase().split('').map(char => baconCode[char] || char).join('');
}

/**
 * Bacon密码转字符串
 * @param {string} str - Bacon密码
 * @returns {string} 原始字符串
 */
export function fromBacon(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const baconCode: Record<string, string> = {
    'AAAAA': 'A', 'AAAAB': 'B', 'AAABA': 'C', 'AAABB': 'D', 'AABAA': 'E', 'AABAB': 'F',
    'AABBA': 'G', 'AABBB': 'H', 'ABAAA': 'I', 'ABAAB': 'J', 'ABABA': 'K', 'ABABB': 'L',
    'ABBAA': 'M', 'ABBAB': 'N', 'ABBBA': 'O', 'ABBBB': 'P', 'BAAAA': 'Q', 'BAAAB': 'R',
    'BAABA': 'S', 'BAABB': 'T', 'BABAA': 'U', 'BABAB': 'V', 'BABBA': 'W', 'BABBB': 'X',
    'BBAAA': 'Y', 'BBAAB': 'Z'
  };
  
  const result: string[] = [];
  for (let i = 0; i < str.length; i += 5) {
    const code = str.substr(i, 5);
    result.push(baconCode[code] || code);
  }
  
  return result.join('');
}

/**
 * 字符串转Base32
 * @param {string} str - 输入字符串
 * @returns {string} Base32字符串
 */
export function toBase32(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  let result = '';
  let bits = 0;
  let value = 0;
  
  for (let i = 0; i < str.length; i++) {
    value = (value << 8) | str.charCodeAt(i);
    bits += 8;
    
    while (bits >= 5) {
      result += alphabet[(value >>> (bits - 5)) & 31];
      bits -= 5;
    }
  }
  
  if (bits > 0) {
    result += alphabet[(value << (5 - bits)) & 31];
  }
  
  while (result.length % 8 !== 0) {
    result += '=';
  }
  
  return result;
}

/**
 * Base32转字符串
 * @param {string} str - Base32字符串
 * @returns {string} 原始字符串
 */
export function fromBase32(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  let result = '';
  let bits = 0;
  let value = 0;
  
  for (let i = 0; i < str.length; i++) {
    if (str[i] === '=') break;
    
    const index = alphabet.indexOf(str[i].toUpperCase());
    if (index === -1) continue;
    
    value = (value << 5) | index;
    bits += 5;
    
    while (bits >= 8) {
      result += String.fromCharCode((value >>> (bits - 8)) & 255);
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base58
 * @param {string} str - 输入字符串
 * @returns {string} Base58字符串
 */
export function toBase58(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  let result = '';
  let num = 0;
  let base = 1;
  
  for (let i = str.length - 1; i >= 0; i--) {
    num += str.charCodeAt(i) * base;
    base *= 256;
  }
  
  while (num > 0) {
    result = alphabet[num % 58] + result;
    num = Math.floor(num / 58);
  }
  
  return result;
}

/**
 * Base58转字符串
 * @param {string} str - Base58字符串
 * @returns {string} 原始字符串
 */
export function fromBase58(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  let num = 0;
  let base = 1;
  
  for (let i = str.length - 1; i >= 0; i--) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    num += index * base;
    base *= 58;
  }
  
  let result = '';
  while (num > 0) {
    result = String.fromCharCode(num % 256) + result;
    num = Math.floor(num / 256);
  }
  
  return result;
}

/**
 * 字符串转Base85
 * @param {string} str - 输入字符串
 * @returns {string} Base85字符串
 */
export function toBase85(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~';
  let result = '';
  
  for (let i = 0; i < str.length; i += 4) {
    let value = 0;
    for (let j = 0; j < 4; j++) {
      if (i + j < str.length) {
        value = (value << 8) | str.charCodeAt(i + j);
      }
    }
    
    for (let j = 4; j >= 0; j--) {
      result += alphabet[Math.floor(value / Math.pow(85, j)) % 85];
    }
  }
  
  return result;
}

/**
 * Base85转字符串
 * @param {string} str - Base85字符串
 * @returns {string} 原始字符串
 */
export function fromBase85(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~';
  let result = '';
  
  for (let i = 0; i < str.length; i += 5) {
    let value = 0;
    for (let j = 0; j < 5; j++) {
      if (i + j < str.length) {
        const index = alphabet.indexOf(str[i + j]);
        if (index === -1) continue;
        value = value * 85 + index;
      }
    }
    
    for (let j = 3; j >= 0; j--) {
      result += String.fromCharCode((value >>> (j * 8)) & 255);
    }
  }
  
  return result;
}

/**
 * 字符串转Base91
 * @param {string} str - 输入字符串
 * @returns {string} Base91字符串
 */
export function toBase91(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base91转字符串
 * @param {string} str - Base91字符串
 * @returns {string} 原始字符串
 */
export function fromBase91(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base92
 * @param {string} str - 输入字符串
 * @returns {string} Base92字符串
 */
export function toBase92(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base92转字符串
 * @param {string} str - Base92字符串
 * @returns {string} 原始字符串
 */
export function fromBase92(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base93
 * @param {string} str - 输入字符串
 * @returns {string} Base93字符串
 */
export function toBase93(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base93转字符串
 * @param {string} str - Base93字符串
 * @returns {string} 原始字符串
 */
export function fromBase93(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base94
 * @param {string} str - 输入字符串
 * @returns {string} Base94字符串
 */
export function toBase94(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base94转字符串
 * @param {string} str - Base94字符串
 * @returns {string} 原始字符串
 */
export function fromBase94(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base95
 * @param {string} str - 输入字符串
 * @returns {string} Base95字符串
 */
export function toBase95(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base95转字符串
 * @param {string} str - Base95字符串
 * @returns {string} 原始字符串
 */
export function fromBase95(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base96
 * @param {string} str - 输入字符串
 * @returns {string} Base96字符串
 */
export function toBase96(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base96转字符串
 * @param {string} str - Base96字符串
 * @returns {string} 原始字符串
 */
export function fromBase96(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base97
 * @param {string} str - 输入字符串
 * @returns {string} Base97字符串
 */
export function toBase97(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base97转字符串
 * @param {string} str - Base97字符串
 * @returns {string} 原始字符串
 */
export function fromBase97(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base98
 * @param {string} str - 输入字符串
 * @returns {string} Base98字符串
 */
export function toBase98(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base98转字符串
 * @param {string} str - Base98字符串
 * @returns {string} 原始字符串
 */
export function fromBase98(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base99
 * @param {string} str - 输入字符串
 * @returns {string} Base99字符串
 */
export function toBase99(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base99转字符串
 * @param {string} str - Base99字符串
 * @returns {string} 原始字符串
 */
export function fromBase99(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base100
 * @param {string} str - 输入字符串
 * @returns {string} Base100字符串
 */
export function toBase100(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base100转字符串
 * @param {string} str - Base100字符串
 * @returns {string} 原始字符串
 */
export function fromBase100(str: string): string {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}
